package com.xizi.Interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * @author xizizzz
 * @description: 在发送前 将value前加时间戳
 * @date 2021-7-3下午 12:32
 */
public class TimeInterceptor implements ProducerInterceptor<String, String> {
    @Override
    public void configure(Map<String, ?> configs) {
    }

    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {
        // 创建一个新的 record，把时间戳写入消息体的最前部
        String value = record.value();
        value = "TimeInterceptor: " + System.currentTimeMillis() + "," + record.value().toString();
        return new ProducerRecord(record.topic(), record.partition(), record.timestamp(), record.key(), value);
    }

    @Override
    public void close() {
    }

    @Override
    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
        // TODO Auto-generated method stub

    }
}
